using System.ComponentModel.Composition;
using System.Text.RegularExpressions;
using OrbitOne.CRMLyncInfoDashboard.CRM;

namespace OrbitOne.CRMLyncInfoDashboard.Shell
{
    [Export(typeof(IActionLinkFactory))]
    public class ActionLinkFactory : IActionLinkFactory
    {
        public static Regex m_EmailAddressPattern = new Regex(@"^[A-Za-z0-9](([_\.\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)\.([A-Za-z]{2,})$");
        public static Regex m_PhoneNumberPattern = new Regex(@"^\+?[\d\s\(\)\.]+$");
        public static Regex m_UrlPattern = new Regex(@"^(http://|https://)?[a-zA-Z0-9\-\.]+\.(.{2,})$");

        public ActionLink GetActionLink(DisplayFieldModel displayFieldModel)
        {
            if (m_EmailAddressPattern.IsMatch(displayFieldModel.FieldValue))
            {
                return new EmailActionLink(displayFieldModel);
            }
            if (m_PhoneNumberPattern.IsMatch(displayFieldModel.FieldValue))
            {
                return new PhoneNumberActionLink(displayFieldModel);
            }
            if (m_UrlPattern.IsMatch(displayFieldModel.FieldValue))
            {
                return new UrlActionLink(displayFieldModel);
            }
            return null;
        }
    }
}